@echo off
setlocal EnableDelayedExpansion

echo ========================================
echo Northstar K-line Data Analysis Tool
echo ========================================
echo.

echo Current directory: %CD%
echo Batch file directory: %~dp0
echo.
@REM
:: Change to project directory
cd /d "%~dp0"
if errorlevel 1 (
    echo [ERROR] Cannot change to project directory
    pause
    exit /b 1
)

echo Checking database file...
:: Check if database file exists
if not exist "data\storage.mv.db" (
    echo.
    echo [WARNING] Database file not found: data\storage.mv.db
    echo.
    echo [HINT] This means Northstar service has never been started!
    echo.
    echo Solution:
    echo 1. Open project in IDEA
    echo 2. Run org.dromara.northstar.NorthstarApplication main method
    echo 3. Wait for "Started NorthstarApplication" log
    echo 4. Visit http://localhost:9999 to confirm service is available
    echo 5. Then run this script again
    echo.
    pause
    exit /b 1
)

:: Get database file size
for %%A in ("data\storage.mv.db") do set dbsize=%%~zA
if not defined dbsize (
    echo [ERROR] Cannot get database file size
    pause
    exit /b 1
)

set /a dbsizeMB=%dbsize% / 1048576

echo [SUCCESS] Found database file: data\storage.mv.db
echo    File size: %dbsizeMB% MB (%dbsize% bytes)
echo.

if %dbsize% LSS 10240 (
    echo [WARNING] Database file is too small -less than 10KB, might be empty!
    echo.
    echo [HINT] This means Northstar service might have failed to start or just created database
    echo.
    echo Solution:
    echo 1. Check if Northstar service is running
    echo 2. Visit http://localhost:9999 to confirm service is available
    echo 3. Create and enable a module in Web interface
    echo 4. Wait a few minutes for system to generate some data
    echo 5. Then run this script again
    echo.
    echo Note: If using SIM gateway, K-line data will NOT be saved to database!
    echo       Please use CTP gateway or check real-time data in frontend interface
    echo.
    pause
    exit /b 1
)

echo Checking northstar-main directory...
if not exist "northstar-main" (
    echo [ERROR] northstar-main directory not found
    echo Current location: %CD%
    pause
    exit /b 1
)

echo Compiling analysis tool...
echo.
cd northstar-main
if errorlevel 1 (
    echo [ERROR] Cannot change to northstar-main directory
    pause
    exit /b 1
)

echo Running: mvn test-compile -DskipTests
call mvn test-compile -DskipTests
if errorlevel 1 (
    echo.
    echo [ERROR] Compilation failed!
    echo.
    pause
    exit /b 1
)

echo.
echo [SUCCESS] Compilation succeeded!
echo.
echo Analyzing database...
echo ========================================
echo.

echo Running: mvn exec:java ...
call mvn exec:java -Dexec.mainClass="org.dromara.northstar.analysis.BarDataAnalyzer" -Dexec.classpathScope=test -q
set EXEC_RESULT=%errorlevel%

echo.
echo ========================================
if %EXEC_RESULT% NEQ 0 (
    echo [ERROR] Execution failed with error code: %EXEC_RESULT%
    echo.
) else (
    echo [SUCCESS] Analysis completed!
    echo.
)

pause
exit /b %EXEC_RESULT%
